package com.gnerv.business.platform.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gnerv.business.platform.common.response.R;
import com.gnerv.business.platform.common.util.UUIDUtil;
import com.gnerv.business.platform.entity.User;
import com.gnerv.business.platform.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 平台用户管理前端控制器
 * </p>
 *
 * @author gnerv
 * @since 2018-06-28
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/platform/user")
public class UserController implements BaseController<User> {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "创建用户", notes = "创建用户")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "account", value = "账号", required = true, paramType = "query"),
        @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query"),
        @ApiImplicitParam(name = "userName", value = "用户名", required = true, paramType = "query")
    })
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Override
    public R create(User user) {
        user.setUserId(UUIDUtil.getLowerUUID());
        boolean b = userService.insert(user);
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "创建部门用户", notes = "创建部门用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", required = true, paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名", required = true, paramType = "query"),
            @ApiImplicitParam(name = "deptId", value = "部门ID", required = true, paramType = "query")
    })
    @RequestMapping(value = "/createDeptUser", method = RequestMethod.POST)
    public R createDeptUser(User user, String deptId) {
        user.setUserId(UUIDUtil.getLowerUUID());
        boolean b = userService.createDeptUser(user, deptId);
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "删除用户", notes = "根据用户id 删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path")
    })
    @RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
    @Override
    public R delete(@PathVariable(value = "userId") String userId) {
        User user = new User();
        user.setUserId(userId);
        boolean b = userService.delete(new EntityWrapper<>(user));
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "更新用户", notes = "根据用户id 更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名", required = true, paramType = "query")
    })
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @Override
    public R update(User user) {
        boolean b = userService.updateByUserId(user);
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "更新用户", notes = "根据用户id 更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名", required = true, paramType = "query")
    })
    @RequestMapping(value = "/updateDeptUser", method = RequestMethod.POST)
    public R updateDeptUser(User user) {
        boolean b = userService.updateByUserId(user);
        return b ? R.ok(R.OK_200) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "查询单个用户", notes = "根据用户id 查询单个用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true, paramType = "path")
    })
    @RequestMapping(value = "/select/{userId}", method = RequestMethod.GET)
    @Override
    public R select(@PathVariable(value = "userId") String userId) {
        User user = new User();
        user.setUserId(userId);
        user = userService.selectOne(new EntityWrapper<>(user));
        return user != null ? R.ok(R.OK_200).data("user", user) : R.error(R.ERROR_500);
    }

    @ApiOperation(value = "查询用户列表", notes = "根据条件 查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "账号", paramType = "query"),
            @ApiImplicitParam(name = "userName", value = "用户名", paramType = "query")
    })
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @Override
    public R list(User user) {
        List<User> users = userService.selectList(new EntityWrapper<>(user));
        return users != null ? R.ok(R.OK_200).data("users", users) : R.error(R.ERROR_500);
    }
}

